/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StudyPanel.java
 *
 * Created on 2012-6-9, 11:33:56
 */

package icexam.exam;

import icexam.ICExamView;
import icexam.exam.print.ReportViewAction;
import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;
import javax.swing.border.Border;

/**
 *
 * @author Ethan
 */
public class StudyPanel extends javax.swing.JPanel {

    /** Creates new form StudyPanel */
    public StudyPanel() {
        initComponents();
    }
    private ICExamView mainView;
    private DefaultListModel listModel = new DefaultListModel();
    public StudyPanel(ICExamView mainView) {
        this();
        this.mainView = mainView;
        stanAnsT.setText("");
        buttonGroup1.add(aSel);
        buttonGroup1.add(bSel);
        aP.setVisible(false);
        this.ctrlP.setVisible(false);
        loadAllQ();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        exP = new javax.swing.JPanel();
        examP = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        examB = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        examB1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        list = new javax.swing.JList();
        aP = new javax.swing.JPanel();
        aSel = new javax.swing.JRadioButton();
        bSel = new javax.swing.JRadioButton();
        ctrlP = new javax.swing.JPanel();
        stanAnsT = new javax.swing.JLabel();
        myAnsT = new javax.swing.JLabel();
        nButton1 = new javax.swing.JButton();
        autoDisplay = new javax.swing.JCheckBox();

        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(800, 600));

        exP.setName("exP"); // NOI18N
        exP.setPreferredSize(new java.awt.Dimension(800, 470));
        exP.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 0));

        examP.setName("examP"); // NOI18N
        examP.setPreferredSize(new java.awt.Dimension(600, 25));
        examP.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 1));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(icexam.ICExamApp.class).getContext().getResourceMap(StudyPanel.class);
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        examP.add(jLabel2);

        examB.setForeground(resourceMap.getColor("examB.foreground")); // NOI18N
        examB.setText(resourceMap.getString("examB.text")); // NOI18N
        examB.setName("examB"); // NOI18N
        examB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                examBActionPerformed(evt);
            }
        });
        examP.add(examB);

        jLabel3.setName("jLabel3"); // NOI18N
        jLabel3.setPreferredSize(new java.awt.Dimension(100, 15));
        examP.add(jLabel3);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        examP.add(jLabel1);

        examB1.setForeground(resourceMap.getColor("examB1.foreground")); // NOI18N
        examB1.setText(resourceMap.getString("examB1.text")); // NOI18N
        examB1.setName("examB1"); // NOI18N
        examB1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                examB1ActionPerformed(evt);
            }
        });
        examP.add(examB1);

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N
        examP.add(jLabel4);

        exP.add(examP);

        jScrollPane1.setName("jScrollPane1"); // NOI18N
        jScrollPane1.setPreferredSize(new java.awt.Dimension(800, 420));

        list.setMaximumSize(new java.awt.Dimension(800, 111111));
        list.setName("list"); // NOI18N
        list.setPreferredSize(null);
        list.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(list);

        exP.add(jScrollPane1);

        aP.setName("aP"); // NOI18N
        aP.setPreferredSize(new java.awt.Dimension(550, 25));
        aP.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        aSel.setFont(resourceMap.getFont("aSel.font")); // NOI18N
        aSel.setText(resourceMap.getString("aSel.text")); // NOI18N
        aSel.setName("aSel"); // NOI18N
        aSel.setPreferredSize(new java.awt.Dimension(250, 27));
        aSel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aSelActionPerformed(evt);
            }
        });
        aP.add(aSel);

        bSel.setFont(resourceMap.getFont("bSel.font")); // NOI18N
        bSel.setText(resourceMap.getString("bSel.text")); // NOI18N
        bSel.setName("bSel"); // NOI18N
        bSel.setPreferredSize(new java.awt.Dimension(250, 27));
        bSel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSelActionPerformed(evt);
            }
        });
        aP.add(bSel);

        exP.add(aP);

        add(exP);

        ctrlP.setName("ctrlP"); // NOI18N
        ctrlP.setPreferredSize(new java.awt.Dimension(580, 30));
        ctrlP.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        stanAnsT.setFont(resourceMap.getFont("stanAnsT.font")); // NOI18N
        stanAnsT.setText(resourceMap.getString("stanAnsT.text")); // NOI18N
        stanAnsT.setName("stanAnsT"); // NOI18N
        stanAnsT.setPreferredSize(new java.awt.Dimension(70, 18));
        ctrlP.add(stanAnsT);

        myAnsT.setName("myAnsT"); // NOI18N
        myAnsT.setPreferredSize(new java.awt.Dimension(120, 22));
        ctrlP.add(myAnsT);

        nButton1.setFont(resourceMap.getFont("nButton1.font")); // NOI18N
        nButton1.setText(resourceMap.getString("nButton1.text")); // NOI18N
        nButton1.setName("nButton1"); // NOI18N
        nButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nButton1ActionPerformed(evt);
            }
        });
        ctrlP.add(nButton1);

        autoDisplay.setText(resourceMap.getString("autoDisplay.text")); // NOI18N
        autoDisplay.setName("autoDisplay"); // NOI18N
        autoDisplay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                autoDisplayActionPerformed(evt);
            }
        });
        ctrlP.add(autoDisplay);

        add(ctrlP);
    }// </editor-fold>//GEN-END:initComponents

    private void nButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nButton1ActionPerformed
        showAnswer();
}//GEN-LAST:event_nButton1ActionPerformed

    private void bSelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSelActionPerformed
        this.buttonGroup1.clearSelection();// TODO add your handling code here:
    }//GEN-LAST:event_bSelActionPerformed

    private void autoDisplayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_autoDisplayActionPerformed
        this.showAnswer();
    }//GEN-LAST:event_autoDisplayActionPerformed

    private void aSelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aSelActionPerformed
        this.buttonGroup1.clearSelection();
}//GEN-LAST:event_aSelActionPerformed

    private void listValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listValueChanged
        int index = list.getSelectedIndex();
        if(index>=0){
            Issue is = (Issue) this.listModel.getElementAt(index);
            aP.setVisible(true);
            this.ctrlP.setVisible(true);
            fillQ(is);
        }

    }//GEN-LAST:event_listValueChanged

    private void examBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_examBActionPerformed
        for(int i=0;i<qs.size();i++){
            qs.get(i).setUserSelIndex(-1);
        }
        Collections.shuffle(qs);
        this.mainView.setMainView(new ExamingPanel(mainView,qs));
    }//GEN-LAST:event_examBActionPerformed

    private void examB1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_examB1ActionPerformed
        try {
//            logger.info("Start preview report.");
            String[][] titles = new String[3][5];
            String[][] objs = new String[50][5];
            titles[0][0] = "字号名称："+this.mainView.getUser().getCompName()
                    + "   负责人 ： "
                    + this.mainView.getUser().getCeoName();
            titles[0][1] = "容县流通环节食品安全知识测试题";
            titles[0][2] = "成绩：";

            titles[1][0] = "";
            titles[1][1] = "试题";
            titles[1][2] = "";
            titles[1][3] = "";
            titles[1][4] = "选择";
//            titles[1][5] = "正确答案";
            System.out.println(objs.length);
            for(int i=0;i<qs.size();i++){
                objs[i][0] = String.valueOf(i+1);
                objs[i][1] = qs.get(i).getIssue();
                if(qs.get(i).getQuestionType()==0){
                    objs[i][2] = "A:"+qs.get(i).getAnswers()[0];
                    objs[i][3] = "B:"+qs.get(i).getAnswers()[1];
                }else{
                    objs[i][2] = "A: 正确";
                    objs[i][3] = "B: 错误";
                }
//                objs[i][2] = "A:"+qs.get(i).getAnswers()[0];
//                objs[i][3] = "B:"+qs.get(i).getAnswers()[1];
                
                    objs[i][4] = "(     )";
//                objs[i][5] = (qs.get(i).getStandardAnswerIndex()==0)?"A":"B";
            }
            ReportViewAction.action(titles, objs,0);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_examB1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel aP;
    private javax.swing.JRadioButton aSel;
    private javax.swing.JCheckBox autoDisplay;
    private javax.swing.JRadioButton bSel;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel ctrlP;
    private javax.swing.JPanel exP;
    private javax.swing.JButton examB;
    private javax.swing.JButton examB1;
    private javax.swing.JPanel examP;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList list;
    private javax.swing.JLabel myAnsT;
    private javax.swing.JButton nButton1;
    private javax.swing.JLabel stanAnsT;
    // End of variables declaration//GEN-END:variables

    private void goToPrefQ() {
        if(qIndex>0){
            qIndex--;
            Issue q = qs.get(qIndex);
            fillQ(q);
        }
    }

    private void goToNextQ() {

        if(qIndex<300-1){
            qIndex++;
            Issue q = qs.get(qIndex);
            fillQ(q);
        }
    }


    private int qIndex = -1;
    private List<Issue> qs = null;
   
    private void loadAllQ() {
        list.setModel(listModel);
        list.setCellRenderer(new Listm(3, 30));
        QuestionHelper tool = new QuestionHelper();
        qs = tool.loaderForExam();
        for(Issue i : qs){
            listModel.addElement(i);
        }
        qIndex = 0;
        Issue q = qs.get(qIndex);
        fillQ(q);
    }

    private class Listm extends JTextArea implements ListCellRenderer {
      protected Listm(int rows, int cols) {
         super(rows, cols);
         this.setLineWrap(true);
//         Border b = BorderFactory.createLineBorder(Color.blue,1);
//         b.
         setBorder(BorderFactory.createLineBorder(Color.blue,1));
      }

      public Component getListCellRendererComponent(JList list, Object value,
               int index, boolean isSelected, boolean cellHasFocus) {
         setText("第 "+(index+1)+" 题    "+value.toString());
         if (cellHasFocus) {
            setBackground(Color.green);
         }else if (isSelected) {
            setBackground(Color.GREEN);
         } else{
             setBackground(null);
         }
//         else if (isSelected) {
//            setBackground(Color.GREEN);
//         } else {
//            setBackground(null);
//         }
         return this;
      }
   }

    private static final String TitleSpace = "   ";
    private static final String A = " A ";
    private static final String B = " B ";
    private static final String ASelKey = " A: ";
    private static final String BSelKey = " B: ";
    private static final String TrueSelKey = " 正确";
    private static final String FalseSelKey = " 错误";
    private Issue currentQ;
    private void fillQ(Issue q) {
        currentQ = q;
        if(q.getQuestionType()==q.Questiong_Select){
            aSel.setText(ASelKey+ q.getAnswers()[0]);
            bSel.setText(BSelKey+ q.getAnswers()[1]);
        }else{
            aSel.setText(ASelKey+ TrueSelKey);
            bSel.setText(BSelKey+ FalseSelKey);
        }
        
        if(autoDisplay.isSelected()){
            this.showAnswer();
        }else{
            buttonGroup1.clearSelection();
            stanAnsT.setText("");
            myAnsT.setText("");
        }
        
    }

    private void showAnswer() {
        stanAnsT.setText("标准答案 ： ");
        switch(this.currentQ.getStandardAnswerIndex()){
            case 0: myAnsT.setText(A);this.aSel.setSelected(true);break;
            case 1: myAnsT.setText(B);this.bSel.setSelected(true);break;
        }
    }

}
